package com.xusheng.cli.controller.back;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.annotation.OperationLogAnno;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.enums.OperationType;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.dictionaryModule.Dictionary;
import com.xusheng.cli.entity.dictionaryModule.SecondaryPassword;
import com.xusheng.cli.service.DictionaryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 字典表 (Dictionary)表控制层
 *
 * @author jy
 * @since 2022-01-17 17:33:02
 */
@Validated
@RestController
@RequestMapping("/api/v1/dictionary")
public class DictionaryController {

    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    /**
     * 通过父字典code 查询子集
     *
     * @param code 字典code
     * @author jy
     * @since 2022/1/18 10:44
     */
    @GetMapping("/listDicByParentCode")
    public Result<List<Dictionary>> listDicByParentCode(@NotBlank(message = "字典code不能为空") String code) {
        return Result.success(dictionaryService.listDicByParentCode(code));
    }

    /**
     * 查询所有数据
     *
     * @param dictionary 查询实体
     * @return 所有数据
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @GetMapping("queryAll")
    public Result<List<Dictionary>> queryAll(Dictionary dictionary) {
        return Result.success(dictionaryService.list(new QueryWrapper<>(dictionary)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param dictionary 查询实体
     * @return 所有数据
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Dictionary>> queryByPage(MyPage<Dictionary> page, Dictionary dictionary) {
        return Result.success(dictionaryService.queryByPage(page, dictionary));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @GetMapping("queryOne/{id}")
    public Result<Dictionary> selectOne(@PathVariable Serializable id) {
        return Result.success(dictionaryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dictionary 实体对象
     * @return 新增结果
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @PostMapping("insert")
    @OperationLogAnno(module = "字典模块", type = OperationType.CREATE, desc = "新增数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Dictionary dictionary) {
        return Result.isSuccess(dictionaryService.save(dictionary));
    }

    /**
     * 修改数据
     *
     * @param dictionary 实体对象
     * @return 修改结果
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @PutMapping("update")
    @OperationLogAnno(module = "字典模块", type = OperationType.UPDATE, desc = "修改数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody Dictionary dictionary) {
        return Result.isSuccess(dictionaryService.updateById(dictionary));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @DeleteMapping("delete/{id}")
    @OperationLogAnno(module = "字典模块", type = OperationType.DELETE, desc = "删除单条数据")
    public Result<Object> delete(@PathVariable Long id) {
        Dictionary dictionary = new Dictionary();
        dictionary.setId(id);
        return Result.isSuccess(dictionaryService.removeById(dictionary));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author jy
     * @since 2022-01-17 17:33:02
     */
    @DeleteMapping("batchDelete")
    @OperationLogAnno(module = "字典模块", type = OperationType.DELETE, desc = "批量删除数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(dictionaryService.batchDelete(idList, new Dictionary()));
    }


    @PostMapping("/set2Pwd")
    public Result<Object> set2Pwd(@RequestBody SecondaryPassword secondaryPassword) {
        String pwd = dictionaryService.get2Pwd();
        if (StrUtil.isNotBlank(pwd)) {
            Assert.isTrue(DigestUtil.bcryptCheck(secondaryPassword.getOldPassword(), pwd), "原密码输入错误!");
        }
        String bcrypt = DigestUtil.bcrypt(secondaryPassword.getPassword());
        return Result.isSuccess(dictionaryService.set2Pwd(bcrypt));
    }

    @PostMapping("/check2Pwd")
    public Result<Object> check2Pwd(@RequestBody SecondaryPassword secondaryPassword) {
        return Result.isSuccess(dictionaryService.check2Pwd(secondaryPassword.getPassword()), "二级密码错误");
    }
}

